/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.DatabaseException;
import sw6.digipecs.model.ExtImage;
import sw6.digipecs.widget.admin.HelpView;
import sw6.digipecs.widget.admin.ImageBrowser;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class AddFromPhoneActivity extends Activity {
	public static final int REQUEST_CODE = 888;
	private ImageBrowser mImageGrid;
	private Context mContext;
	private HelpView mHelpView;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addfromphonelayout); 
		mImageGrid = (ImageBrowser) findViewById(R.id.addFromPhoneGrid);
		mContext = getApplicationContext();
		mHelpView = (HelpView)findViewById(R.id.helpAddFromPhone); 
		mHelpView.setHeadText("Tilføj Billeder fra Enheden"); 
		mHelpView.setBodyText("Her vises alle billeder der findes på enheden. Marker dem blot med fingeren. De vil så blive tilføjet så snart der trykkes på tilbageknappen"); 
		mImageGrid.setUp(this); 
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        savePictures(); 
	        this.setResult(RESULT_OK); 
	        this.finish(); 
	    }
	    return super.onKeyDown(keyCode, event);
	}
	private void savePictures() {
		ArrayList<String> selectedItems = mImageGrid.getSelected(); 
		int i = 0; 
		for(String s:selectedItems)
		{
			try {
				ExtImage.CreateNew(mContext, s, "", "");
			} catch (DatabaseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			i++; 
		}
		Toast.makeText(mContext, Integer.toString(i)+" billeder blev tilføjet", Toast.LENGTH_SHORT).show(); 
	}
}
